#ifndef __LOG_H__
#define __LOG_H__

#include <cstdio>
#include <ctime>
#include "fiber.h"

typedef enum LOGLEVEL
{
    DEBUG,
    INFO,
    WARNNING,
    ERROR,
    FATAL
} LOGLEVEL;

inline const char *level_to_string(LOGLEVEL level)
{
    switch (level)
    {
    case DEBUG:
        return "DEBUG";
    case INFO:
        return "INFO";
    case WARNNING:
        return "WARNNING";
    case ERROR:
        return "ERROR";
    case FATAL:
        return "FATAL";
    default:
        return nullptr;
    }
}

/**
 * @brief 日志宏函数,支持指定日志打印等级
 * @details 日志信息以格式化字符串形式输出到stdout,输出格式为: [日志等级][线程ID][协程ID][年-月-日 时-分-秒][文件名:行号][具体日志信息]
 */

#define LOG(level, format, ...)                                                                                                \
    do                                                                                                                         \
    {                                                                                                                          \
        const char *levelstr = level_to_string(level);                                                                         \
        time_t ts = time(nullptr);                                                                                             \
        struct tm *lt = localtime(&ts);                                                                                        \
        char timebuf[32] = {0};                                                                                                \
        strftime(timebuf, sizeof(timebuf) - 1, "%y-%m-%d %H-%M-%S", lt);                                                       \
        fprintf(stdout, "[%s][%p][%llu][%s][%s:%d] " format "\n",                                                              \
                levelstr, (void *)pthread_self(), Fiber::GetThis()->GetFiberId(), timebuf, __FILE__, __LINE__, ##__VA_ARGS__); \
    } while (0)


#endif